package com.hopedove.returnyarnsystem.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


import com.hopedove.base.BaseActivity;
import com.hopedove.net.ErrResponse;
import com.hopedove.net.OkResponse;
import com.hopedove.net.domain.Communication;
import com.hopedove.net.domain.GetReturnYarnInfoBatch;
import com.hopedove.returnyarnsystem.R;
import com.hopedove.returnyarnsystem.constants.Constants;
import com.hopedove.utils.ToastUtil;
import com.hopedove.view.TipsDialogFragment;

import java.util.HashMap;

@SuppressLint("UseSparseArrays")
public class InputBatchNoActivity extends BaseActivity implements OnClickListener {
    public static String SOURCE_DATA = "source_data";
    private EditText barCode;
    private TipsDialogFragment tipDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_input_batchno);
        barCode = findViewById(R.id.barcode_et);
        barCode.setText("");
        findViewById(R.id.confirm_barcode_img).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        if (v.getId() == R.id.confirm_barcode_img) {

            String jtNo = barCode.getText().toString().trim();
            if (jtNo == null || jtNo.equals("")) {
                Toast.makeText(this, "请先输入批号", Toast.LENGTH_LONG).show();
                return;
            }

            View view = getLayoutInflater().inflate(R.layout.loading, null);
            tipDialog = TipsDialogFragment.newInstance(view);
            TextView textView = view.findViewById(R.id.tip);
            String tipStr = "正在获取信息...";
            textView.setText(tipStr);
            tipDialog.setCancelable(false);
            tipDialog.show(getSupportFragmentManager(), "");
            getReturnYarnInfo();
        }
    }


    @Override
    protected void onResume() {
        barCode.setText("");
        super.onResume();
    }

    /**
     * 获取纱单详情信息
     */
    void getReturnYarnInfo() {

        GetReturnYarnInfoBatch getReturnYarnInfoBatch = new GetReturnYarnInfoBatch(barCode.getText().toString());
        Communication.asyncRequest(getReturnYarnInfoBatch, (r) -> {
            TipsDialogFragment.dismissTipDialog(tipDialog);

            if (r instanceof OkResponse) {
                Intent intent = new Intent(InputBatchNoActivity.this,
                        MaterialInfoForBatchNoActivity.class);
                intent.putExtra(Constants.JT_NO, barCode.getText().toString());
                intent.putExtra(SOURCE_DATA, ((OkResponse) r).getData());
                startActivity(intent);
            } else if (r instanceof ErrResponse) {
                ToastUtil.longToast(getBaseContext(), ((ErrResponse) r).getErrText());
            }
        });
    }

}
